package com.ostrich.library_log

import com.ostrich.library_logger.logger.*

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/23 14:55
 * @Description : Logger
 */
class Logger private constructor(private val tag:String,private val isDebug:Boolean,private val level: LoggerLevel,private val url: String,private val loggerStrategy:LoggerStrategy){
    private var logger: ILogger //真正的logger对象根据不同策略创建不同的logger对象
    init {
        logger = initLogger()
    }
    //策略者
    private fun initLogger(): ILogger {
        logger =   when(loggerStrategy){
            LoggerStrategy.LOGCAT -> LogCatLogger()
            LoggerStrategy.NET -> NetWorkLogger()
            LoggerStrategy.FILE -> FileLogger()
            LoggerStrategy.EMAIL -> EmailLogger()
        }
        logger.setLoggerLevel(level)
        logger.setLogTag(tag)
        logger.setDebug(isDebug)
        logger.setSaveUrl(url)
        return logger
    }

    fun d(tag:String,log:String){
        logger.d(tag,log)
    }
    fun v(tag:String,log:String){
        logger.v(tag,log)
    }
    fun i(tag:String,log:String){
        logger.i(tag,log)
    }
    fun w(tag:String,log:String){
        logger.w(tag,log)
    }
    fun e(tag:String,log:String){
        logger.d(tag,log)
    }







    //构建者
    constructor(builder: Builder):this(
        builder.tag,
        builder.isDebug,
        builder.level,
        builder.url,
        builder.loggerStrategy
    )


    //构建者
    class Builder{
        var isDebug: Boolean = true
        var tag:String = ""
        var level: LoggerLevel = LoggerLevel.Debug
        var url: String = ""
        var loggerStrategy:LoggerStrategy = LoggerStrategy.LOGCAT  //日志输出策略


        fun setDebug(isDebug: Boolean):Builder{
            this.isDebug = isDebug
            return this
        }
        fun setTag(tag:String): Builder {
            this.tag = tag
            return this
        }
        fun setLevel(level: LoggerLevel ): Builder {
            this.level = level
            return this
        }

        fun setSaveUrl(url: String ): Builder {
            this.url = url
            return this
        }

        fun setStrategy(loggerStrategy:LoggerStrategy ): Builder {
            this.loggerStrategy = loggerStrategy
            return this
        }
        fun build():Logger{
            return Logger(this)
        }
    }


}